import React, { Component } from 'react';
import {category_list} from "../uilt/api"
import context from "../uilt/context"
class MyLeft extends Component {
  constructor(props){
      super(props);
      this.state={
        navlist:[],
        currentIndex:0
      }
  }
  handelClick(itemID,index){
    this.setState({
 currentIndex:index
    },()=>{
    })
    context.emit("savaid",itemID,index)
  }
componentDidMount(){
  category_list().then((res)=>{
    console.log(res.data);
     this.setState({
      navlist:res.data.list
     })
  })
}
    render() {
        return (
            <div className="cateleft">
            {this.state.navlist.map((item,index)=>{
              return (
                // 当一个表达式中同时出现加号和？就有优先级问题所以要用括号括起来
                <div onClick={()=>{this.handelClick(item.id,index)}} className={"name "+(this.state.currentIndex===index?"active":"")} key={index}>{item.name}</div>
              )
            })}
          </div>
        );
    }
}

export default MyLeft;